#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>
int main()
{
	int pipefd[2];
	int result = pipe(pipefd);
	if(result != 0 )
	{
		perror("pipe");
		exit(1);
	}
	pid_t id = fork();
	if(id > 0)
	{
		//father
		close(pipefd[0]);
		const char* buffer = "i am father\n";
		ssize_t  w = write(pipefd[1],buffer,strlen(buffer));
		if(w != strlen(buffer))
		{
			perror("write");
			close(pipefd[1]);
			exit(1);
		}
		waitpid(id,NULL,0);

	}
	else if(id == 0)
	{
		//child
		close(pipefd[1]);
		char str[1024];
		ssize_t r = read(pipefd[0],str,sizeof(str));
		if(r == -1)
		{
			perror("read");
			close(pipefd[0]);
			exit(1);
		}
		printf("%s",str);

	}
	else
	{
		perror("fork");
		close(pipefd[0]);
		close(pipefd[1]);

		exit(1);
	}
	return 0;
}
